package com.android.pdd.baseapp.hook;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public abstract class BaseDialog extends Dialog {
    protected View dialogView;
    protected WindowManager.LayoutParams params;
    protected Context context;

    public BaseDialog(Context context) {
        super(context);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        this.context = context;
        init();
    }

    private void init() {
        dialogView = provideView();
        setContentView(dialogView);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    protected abstract View provideView();

    protected abstract void initView();

    protected abstract void initData();

    public int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getSize(point);
        }
        return point.x;
    }

    /**
     * Return the height of screen, in pixel.
     *
     * @return the height of screen, in pixel
     */
    public int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            //noinspection ConstantConditions
            wm.getDefaultDisplay().getSize(point);
        }
        return point.y;
    }

    /**
     * 宽充满屏幕
     *
     * @return
     */
    public BaseDialog fullWidth() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 高充满屏幕
     *
     * @return
     */
    public BaseDialog fullHeight() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 高充满屏幕
     *
     * @return
     */
    public BaseDialog fullScreen() {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return this;
    }


    /**
     * 位置在屏幕最底部
     *
     * @return
     */
    public BaseDialog bottom() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 位置在屏幕最底部
     *
     * @return
     */
    public BaseDialog bottom(int marginBottom) {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.BOTTOM;
        params.y = marginBottom;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 位置在右上角
     *
     * @return
     */
    public BaseDialog rightTop() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.RIGHT | Gravity.TOP;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 位置在右上角
     *
     * @return
     */
    public BaseDialog leftTop() {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.gravity = Gravity.LEFT | Gravity.TOP;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 设置宽度
     *
     * @return
     */
    public BaseDialog setWidth(int width) {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.width = width;
        getWindow().setAttributes(params);
        return this;
    }

    /**
     * 设置高度
     *
     * @return
     */
    public BaseDialog setHeight(int height) {
        if (params == null) {
            params = getWindow().getAttributes();
        }
        params.height = height;
        getWindow().setAttributes(params);
        return this;
    }


    public int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
